#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
/*
    shmget
    shmctl
    shmat
    shmdt
*/
#define ERR_EXIT(m)\
do\
{\
    perror(m);\
    exit(EXIT_FAILURE);\
}while(0)

const char* PATHNAME = "./test.cc";
const int PROJ_ID = 1;

int main(){
    key_t key = ftok(PATHNAME,PROJ_ID);
    if(key == -1)ERR_EXIT("ftok");
    const size_t shm_size = sizeof(int); 
    int shmid = shmget(key,shm_size,IPC_CREAT|IPC_EXCL|0666);
    if(shmid == -1)ERR_EXIT("shmget");
    printf("SHMGET SUCCESS\n");

    int* pnum = (int*)shmat(shmid,NULL,0);
    if(pnum == (void*)-1)ERR_EXIT("shmat");
    *pnum = 0;
    printf("SHMAT SUCCESS\n");
    for(int i = 0;i < 10;i++){
        printf("shm_num:%d\n",*pnum);
        (*pnum)++;
    }

    shmdt(pnum);

    return 0;
}
